import Vue from "vue";

/**
 * 初始化数据
 * @param self
 * @param tiyData
 */
export const init = (self, tiyData) => {
  self.$apiEmail.getTagList(null)
    .then(function (response) {
      if (response.success) {
        tiyData.tData.tagList.splice(0,tiyData.tData.tagList.length);
        for(let i=0;i<response.result.length;i++){
          tiyData.tData.tagList.push(response.result[i]);
        }
      }
    });
}



/**
 * 查询管理标签
 * @param self
 * @param tiyData
 */
export const emailList = (self, tiyData) => {
  self.loadingData = true;
  self.$Spin.show();
  self.$ySpliceAll(tiyData.tData.emailList);

  let param = {
    "pageSize": tiyData.tPage.stepNumber,
    "pageNum": tiyData.tPage.currentPage,
    "subject": tiyData.tParam.subject.trim(),
    "state":1,
    "tags": tiyData.tParam.tags,
    "startTime": tiyData.tParam.startTime,
    "endTime": tiyData.tParam.endTime,
    "encodeName":tiyData.tParam.userNo.trim(),
    "isAll":true,
    "sort":self.sort,
    "sysEmail":1,
  };

  if(tiyData.tParam.startTime){
    param.startTime = Vue.prototype.$tiyQueryTime(new Date(tiyData.tParam.startTime).getTime());
  }
  if(tiyData.tParam.endTime){
    param.endTime = Vue.prototype.$tiyQueryTime(new Date(tiyData.tParam.endTime).getTime());
  }

  self.$apiEmail.queryParseEmailList(param).then(function (response) {
    if(response.success){
      let emailPage = response.result;
      tiyData.tPage.total = emailPage.total;
      tiyData.tPage.stepNumber = emailPage.pageSize;
      emailPage.list.forEach(
        function (item) {
          tiyData.tData.emailList.push(item);
        }
      );
    }else{
      self.$Message.error(response.errorMessage);
    }
    self.$Spin.hide();
  });
};

/**
 * 查询详情
 * @param self
 * @param tiyData
 */
export const showEmail = (self, tiyData,index) => {
  tiyData.tParam.email.content='';
  tiyData.tParam.email.attachment={};
  let emailInfo=tiyData.tData.emailList[index];
  let param = {
    "id": emailInfo.id,
    "type": "parse",
    "yunKey": emailInfo.yunKey,
  };
  self.$apiEmail.queryEmailDetail(param).then(function (response) {
    if(response.success){
      let emailParse = response.result;
      tiyData.tParam.email.content=emailParse.content;
      tiyData.tParam.email.attachment=emailParse.attachmentList;
      if(emailParse.attachmentList.length>0){
        tiyData.tParam.email.attachmentShow=true;
        tiyData.tParam.email.attachmentCount=`附件 (${emailParse.attachmentList.length}个)`;
      }else{
        tiyData.tParam.email.attachmentShow=false;
      }
    }else{
      self.$Message.error(response.errorMessage);
    }
    self.$Spin.hide();
  });
  tiyData.tParam.email.id=emailInfo.id;
  tiyData.tParam.email.emailAddressId=emailInfo.emailAddressId;
  tiyData.tParam.email.emailTo=emailInfo.emailTo;
  tiyData.tParam.email.createTime=emailInfo.createTime;
  tiyData.tParam.email.subject=emailInfo.subject;
  tiyData.tParam.email.virtualName=emailInfo.virtualName;
  tiyData.tParam.email.encodeName=emailInfo.encodeName!=null?emailInfo.encodeName:"";

  tiyData.tParam.email.tags=[];
  if(emailInfo.tags!=null&&emailInfo.tags!=""){
    tiyData.tParam.email.tags=self.$toJson(emailInfo.tags);
  }

  //加载历史邮件列表
  self.$refs.AEmailDetail.emailHistoryCount(emailInfo.virtualName);
  tiyData.tStatus.showEmail.isShowing=true;
}

/**
 * 保存管理标签详情
 * @param self
 * @param tiyData
 */
export const save = (self, tiyData) => {
  self.$refs['tabForm'].validate((valid) => {
    if (valid) {
      self.$Spin.show();
      self.$apiOther.saveManageTab(tiyData.tData.tab).then(function(response){
        self.$Spin.hide();
        if(response.success){
          listTabs(self, tiyData);
          self.$Message.success('保存成功');
          tiyData.tStatus.addModal.isShow=false;
        }else{
          self.$Message.error(response.errorMessage);
        }
      })
     }else{
       changeModelLoading(self, tiyData);
    }
  })
};

/**
 * 删除
 * @param self
 * @param tiyData
 */
export const deleted = (self, tiyData,index) => {
  tiyData.tStatus.modalDelete.isShow = true;
};

export const webDelete = function (self, tiyData) {
  tiyData.tStatus.modalDelete.isLoading = true;
  let params={
    id: tiyData.tStatus.modalDelete.email.id,
    state:-1,
  }
  self.$apiEmail.updateEmailProcess(params)
    .then(function (response) {
      tiyData.tStatus.modalDelete.isShow = false;
      changeModelLoading(self, tiyData);
      if (response.success) {
        emailList(self, tiyData)
        self.$Message.success('删除成功!');
      } else {
        self.$Message.error(response.errorMessage);
      }
    });
};

export const webDeleteAll = function (self, tiyData) {
  tiyData.tStatus.modalDelete.isLoading = true;
  let params={
    ids: self.messageSelected,
    state:-1,
  }
  self.$apiEmail.updateEmailLBatch(params)
    .then(function (response) {
      tiyData.tStatus.modalDelete.isShow = false;
      changeModelLoading(self, tiyData);
      if (response.success) {
        self.messageSelected.splice(0,self.messageSelected.length);
        emailList(self, tiyData);
        self.$Message.success('删除成功!');
      } else {
        self.$Message.error(response.errorMessage);
      }
    });
};

/**
 * 初始化tab数据
 * @param tab
 */
export const resetTabData = (tab) => {
  tab.id = "";
  tab.name = "";
  tab.type = 0;
  tab.typeName = "";
  tab.noSeo = false;
  tab.description = "";
  tab.createtime = "";
};

export const spam = (self, tiyData,index) => {
  let emailInfo=tiyData.tData.emailList[index];
  let params={
    id: emailInfo.id,
    state:2,
  }
  self.$Spin.show();
  self.$apiEmail.updateEmailProcess(params).then(response => {
    self.$Spin.hide();
    if(response.success) {
      self.$Message.success("操作成功!");
      tiyData.tStatus.showEmail.isShowing=false;
      self.search();
    }else{
      self.$Message.error(response.errorMessage);
    }
  });
};

export const addBlacklist = (self, tiyData,index) => {
  let emailInfo=tiyData.tData.emailList[index];
  if(null == emailInfo.virtualName||"" == emailInfo.virtualName){
    self.$Message.error("拉黑人邮箱不能为空");
    return;
  }
  let params={
    userEmail: emailInfo.virtualName,
    userName:emailInfo.senderName,
    type:1,
  }
  self.$Spin.show();
  self.$apiEmail.addBlacklist(params).then(response => {
    self.$Spin.hide();
    if(response.success) {
      self.$Message.success("操作成功!");
      tiyData.tStatus.showEmail.isShowing=false;
      self.search();
    }else{
      self.$Message.error(response.errorMessage);
    }
  });
};

/**
 * 获取标签类型名称
 * @param type
 * @returns {string}
 */
export const replyEmail=(self, tiyData,index)=>{
  let emailInfo=tiyData.tData.emailList[index];
  const original=`<div><p> </p><p> </p><p> </p><p> </p>
                ------------------ original ------------------
                <p><strong>From:</strong>${emailInfo.virtualName}</p>
                <p><strong>Date:</strong>${emailInfo.createTime}</p>
                <p><strong>To:</strong>${emailInfo.emailTo}</p>
                <p><strong>Subject:</strong>${emailInfo.subject}</p></div>`;
  tiyData.tParam.email.id=emailInfo.id;
  tiyData.tParam.email.selectedUsers=[];
  let item={
    contactEmail:emailInfo.virtualName,
    fullEmail:emailInfo.virtualName,
  };
  tiyData.tParam.email.selectedUsers.push(item);
  if(emailInfo.subject.indexOf("Re:")==-1&&emailInfo.subject.indexOf("回复:")==-1){
    self.$refs.SendEmail.email.title=`Re: ${emailInfo.subject}`;
  }else{
    self.$refs.SendEmail.email.title=emailInfo.subject;
  }

  self.$refs.SendEmail.email.content=`${original}<p> </p>${tiyData.tParam.email.content}`;

  self.$refs.SendEmail.$data.fileList.splice(0,self.$refs.SendEmail.$data.fileList.length);
  tiyData.tStatus.showEmail.isShowing=false;
  tiyData.tStatus.replayEmail.isShowing=true;
}

function changeModelLoading(self, tiyData) {

  tiyData.tStatus.modalDelete.isLoading = false;
  self.$nextTick(function () {
    tiyData.tStatus.modalDelete.isLoading = true;
  });

}
